package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import stuff.FixedSizeDocument;

public class GUIPort extends JDialog {
	private static final long serialVersionUID = 1L;
	private JTextField tfPort;
	private JButton btOK,btCancel;
	private int port;
	
	public GUIPort(JFrame parent) {
		super(parent,"Thiết lập Port", true);
		
		JPanel portPanel = new JPanel(new GridBagLayout());
		portPanel.setOpaque(false);
		
		JLabel lpPort;
		lpPort = new JLabel();
		lpPort.setText("Port :  ");
		lpPort.setFont(lpPort.getFont().deriveFont(16f));
		lpPort.setForeground(new Color(40,40,40));
		
		tfPort = new JTextField();
		tfPort.setDocument(new FixedSizeDocument(20));
		tfPort.setPreferredSize(new Dimension(0, tfPort.getPreferredSize().height +6));
		tfPort.setFont(tfPort.getFont().deriveFont(16f));
		tfPort.setForeground(new Color(40,40,40));
		
		GridBagConstraints c= new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 0;
		portPanel.add(lpPort,c);
		c.gridx = 1;
		c.weightx = 0.7f;
		portPanel.add(tfPort,c);
		
		c.gridx = 1;
		c.gridy = 1;
		portPanel.add(Box.createVerticalStrut(6),c);
		
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		portPanel.setOpaque(true);
		
		btOK = new CustomJButton("Đồng ý", 110, 30, Color.WHITE, CustomJButton.DARKER, 
				Color.DARK_GRAY, 2, false, true);
		btOK.addActionListener(new OKAction());
		
		btCancel = new CustomJButton("Thoát", 90, 30, Color.WHITE, CustomJButton.DARKER, 
				Color.DARK_GRAY, 2, false, true);
		btCancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		
		buttonPanel.add(btOK);
		buttonPanel.add(btCancel);
		
		JPanel displayPanel = new JPanel(new BorderLayout());
		displayPanel.setOpaque(false);
		displayPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
		displayPanel.add(portPanel,BorderLayout.CENTER);
		displayPanel.add(buttonPanel,BorderLayout.SOUTH);
		
		add(displayPanel);
		
		getPort();
	}
	
	public void getPort(){
		File ipFile = new File("portConfig.dbz");
		if(ipFile.exists()){
			try {
				DataInputStream is = new DataInputStream(new FileInputStream(ipFile));
				port = is.readInt();
				is.close();
			} catch (IOException e) {
				JOptionPane.showMessageDialog(this, "Có lỗi xảy ra khi đọc tập tin portConfig.dbz",
						"Thông báo", JOptionPane.ERROR_MESSAGE);
				port = 27000;
			}
		}
		else{
			JOptionPane.showMessageDialog(this, "Không tìm thấy thiết lập có sẵn.",
					"Thông báo", JOptionPane.WARNING_MESSAGE);
			port = 27000;
		}
		tfPort.setText(String.valueOf(port));
	}
	
	class OKAction extends AbstractAction{
		
		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent e) {
			if(tfPort.getText().trim().equals("")){
				JOptionPane.showMessageDialog(GUIPort.this, "Port không được để trống.", 
						"Thông báo", JOptionPane.WARNING_MESSAGE);
				return;
			}

			try {
				int port = Integer.parseInt(tfPort.getText());
				DataOutputStream os = new DataOutputStream(new FileOutputStream("portConfig.dbz"));
				os.writeInt(port);
				os.flush();
				os.close();
				JOptionPane.showMessageDialog(GUIPort.this, "Thiết lập thành công.",
						"Thông báo", JOptionPane.INFORMATION_MESSAGE);
				dispose();
			} 
			catch (IOException ea) {
				JOptionPane.showMessageDialog(GUIPort.this, "Thiết lập không thành công.",
						"Thông báo", JOptionPane.WARNING_MESSAGE);
				return;
			}catch (NumberFormatException ea){
				JOptionPane.showMessageDialog(GUIPort.this, "Port chỉ có thể chứa số.",
						"Thông báo", JOptionPane.WARNING_MESSAGE);
				return;
			}
		}
	}
}